package org.tigris.atlas.quizme.client.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.tigris.atlas.quizme.client.form.AnswerForm;
import org.tigris.atlas.quizme.client.utils.Constants;
import org.tigris.atlas.quizme.transfer.Answer;
import org.tigris.atlas.quizme.transfer.Question;
import org.tigris.atlas.quizme.transfer.Quiz;

/**
 * This action executes when the user selects one or more answers and hits submit.  
 * The action determines if the selected answers are correct or not then records 
 * the user reponse in the session.
 * 
 * @author Robert Simmering
 */
public class AnswerAction  extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
		      HttpServletRequest request, HttpServletResponse response) {	
		Question question = (Question)request.getSession().getAttribute(Constants.CURRENT_QUESTION_KEY);
		Quiz quiz = (Quiz)request.getSession().getAttribute(Constants.QUIZ_KEY);
		List answers = (List)request.getSession().getAttribute(Constants.CURRENT_ANSWERS_KEY);
		
		List userAnswers = (List)request.getSession().getAttribute(Constants.ANSWERS_KEY);
		if(userAnswers==null){
			userAnswers = new ArrayList(quiz.getNumQuestions().intValue());
			request.getSession().setAttribute(Constants.ANSWERS_KEY, userAnswers);
		}
		
		Boolean isMultiAnswer = question.getMultiAnswer();
		boolean bIsMultiAnswer = (isMultiAnswer==null)?false:isMultiAnswer.booleanValue();
		
		int totalCorrectAnswerCount=0;
		int userCorrectAnswerCount=0;
		AnswerForm userAnswer = new AnswerForm();
		
		int index=0;
		
		Iterator i= answers.iterator();
		while(i.hasNext()){
			Answer realAnswer = (Answer)i.next();
			String userEnteredAnswer = request.getParameter("answer"+index);
			if(!StringUtils.isBlank(userEnteredAnswer) && userEnteredAnswer.equals("on")){
				// Append this answer to the form
				if(!StringUtils.isBlank(userAnswer.getAnswer())){
					userAnswer.setAnswer(userAnswer.getAnswer()+","+index);
				}
				else{
					userAnswer.setAnswer(""+index);
				}
				if(realAnswer.getCorrect()!= null && realAnswer.getCorrect().booleanValue()){
					userCorrectAnswerCount++;
				}
				
			}	
			if(realAnswer.getCorrect()!= null && realAnswer.getCorrect().booleanValue()){
				totalCorrectAnswerCount++;
			}
			index++;
		}
		if((!bIsMultiAnswer && userCorrectAnswerCount==1) || (bIsMultiAnswer && totalCorrectAnswerCount == userCorrectAnswerCount)){
			userAnswer.setIsCorrect("true");
			quiz.setNumCorrect(new Integer(quiz.getNumCorrect().intValue()+1));
		}
		else{
			userAnswer.setIsCorrect("false");
		}
		userAnswer.setQuestion(question.getQuestion());
		userAnswer.setQuestionId(question.getQuestionId());
		userAnswers.add(userAnswer);
		
		return mapping.findForward("success");
	}
}
